/* eslint-disable prettier/prettier */
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
let width, height, camera, renderer, scene, controls, allMeshGroup;
const initThreeScene = (position, lookat, mapdata) => {
  scene = new THREE.Scene();

  width = window.innerWidth - mapdata.width;
  height = window.innerHeight - mapdata.height;
  camera = new THREE.PerspectiveCamera(45, width / height, 10, 5000); //摄像机对象
  renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true,
  });
  allMeshGroup = new THREE.Group();
  allMeshGroup.name = '精灵模型';
  controls = new OrbitControls(camera, renderer.domElement);
  // 点光源
  // const point = new THREE.PointLight(0xffffff, 0.8);
  // point.position.set(0, 800, 0);
  // scene.add(point);
  // 环境光
  const ambient = new THREE.AmbientLight(0xffffff, 0.6);
  scene.add(ambient);
  // 平行光
  var dtLight = new THREE.DirectionalLight(0xffffff, 0.4);
  dtLight.position.set(0, 100, 0);
  scene.add(dtLight);
  // 半球光
  var light = new THREE.HemisphereLight(0xffffff, 0x000000, 0.12);
  light.position.set(0, 1, 0);
  scene.add(light);
  // 辅助坐标
  // const axesHelper = new THREE.AxesHelper(500);
  // scene.add(axesHelper);
  // 修改相机,场景的参数
  camera.position.set(position.x, position.y, position.z);
  camera.lookAt(lookat.x, lookat.y, lookat.z);
  controls.target = new THREE.Vector3(lookat.x, lookat.y, lookat.z);
  controls.enableDamping = true; // 使动画循环使用时阻尼或自转
  controls.dampingFactor = 0.6; // 动态阻尼系数 就是鼠标拖拽旋转灵敏度
  controls.enableRotate = true; // 是否可以旋转
  controls.enableZoom = 3500; // 最大距离
  controls.maxPolarAngle = Math.PI / 2.5; // 控制上下旋转最大角度
  // controls.minPolarAngle = Math.PI / 2; // 控制上下旋转最小角度
  controls.panSpeed = 2; // 位移速度
  controls.minDistance = 100; // 设置相机距离原点的最近距离
  controls.maxDistance = 2500; // 设置相机距离原点的最远距离
  controls.enablePan = true; // 是否开启右键拖拽
  //render的相关设置 .清晰度乘以3场景更清晰
  renderer.setPixelRatio(window.devicePixelRatio * 1.5);
  renderer.setSize(width, height);
  renderer.setClearColor(0xe5e7ed, 1);
  return renderer.domElement;
};
const onWindowResize = (mapdata) => {
  width = window.innerWidth - mapdata.width; // 窗口宽度
  height = window.innerHeight - mapdata.height; //窗口度
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
  renderer.setSize(width, height);
};
const Render = () => {
  requestAnimationFrame(Render);
  controls.update();
  // console.log(camera.position);
  // renderer.clear();
  renderer.render(scene, camera);
};

export {
  scene,
  camera,
  renderer,
  controls,
  initThreeScene,
  Render,
  onWindowResize,
  allMeshGroup,
};
